<template>
	<div class="right-box">
		<Breadcrumb style="margin-bottom: 5px;">
	        <BreadcrumbItem>
	            <Icon type="ios-people" color="red" size="18"></Icon> 系统管理
	        </BreadcrumbItem>
	        <BreadcrumbItem>
	           部门管理
	        </BreadcrumbItem>
	    </Breadcrumb>
	    <Button class="addBtn" @click="addDepartmentModel"  v-if="userType==0">新增部门</Button>
	    <Input search enter-button="搜索" placeholder="请输入部门名字..." v-model="search" class="search-input" @on-search="goSearch"/>
	    <Table height="480" :columns="columns1" :data="datas">
	    	<template slot-scope="{ row, index }" slot="action">
	            <Button type="primary" style="margin-right: 5px" @click="edit(row)"  v-if="userType==0">修改</Button>
	            <Button type="error" @click="remove(row)">删除</Button>
	        </template>
	    </Table>
	    <Modal
        title="请按要求完善表单"
        v-model="modelShow"
        :closable="false" @on-ok="checkSure" @on-cancel="handleReset('formValidate')" :styles="{top: '20px'}">
	        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
		        <FormItem label="部门名称" prop="name">
		            <Input v-model="formValidate.name" placeholder="请输入部门名称"></Input>
		        </FormItem>
		        <FormItem label="部门负责人" prop="manager">
		            <Select v-model="formValidate.manager">
		                <Option :value="list.userId" v-for="(list,index) in managerList" :key="index">{{list.nickName}}</Option>
		            </Select>
		        </FormItem>
		    </Form>
	    </Modal>
	    <Modal
        title="请按要求完善表单"
        v-model="modelEdit"
        :closable="false" @on-ok="checkSure1" @on-cancel="handleReset('formValidate1')" :styles="{top: '20px'}">
	        <Form ref="formValidate1" :model="formValidate1" :rules="ruleValidate1" :label-width="100">
		        <FormItem label="部门名称" prop="name">
		            <Input v-model="formValidate1.name" placeholder="请输入部门名称"></Input>
		        </FormItem>
		        <FormItem label="部门负责人" prop="manager">
		            <Select v-model="formValidate1.manager">
		                <Option :value="list.userId" v-for="(list,index) in managerList" :key="index">{{list.nickName}}</Option>
		            </Select>
		        </FormItem>
		    </Form>
	    </Modal>
	</div>
</template>

<script>
	export default{
		name:'departmentManager',
		data(){
			return{
				search:'',
				modelShow:false,
				modelEdit:false,
				formValidate: {
                    name: '',
                    manager:''
                },
                ruleValidate: {//表单校验规则
                    name: [
                        { required: true, message: '部门名称不能为空', trigger: 'blur' }
                    ],
                    manager: [
                        { required: true, message: '部门负责人不能为空', trigger: 'blur' }
                    ]
                },
                formValidate1: {
                    name: '',
                    manager:''
                },
                ruleValidate1: {//表单校验规则
                    name: [
                        { required: true, message: '部门名称不能为空', trigger: 'blur' }
                    ],
                    manager: [
                        { required: true, message: '部门负责人不能为空', trigger: 'blur' }
                    ]
                },
				columns1: [//表头
                    {
                        title: '部门编号',
                        key: 'departmentId'
                    },
                    {
                        title: '部门名称',
                        key: 'departmentName'
                    },
                    {
                        title: '部门负责人',
                        key: 'departmentPrincipalName'
                    },
                    {
                        title: '创建时间',
                        key: 'createTime'
                    },{
                    	title:'操作',
                    	slot:'action'
                    }
                ],
				datas:[],//表格数据
				managerList:[],
				userType:0
			}
		},
		methods:{
			addDepartmentModel(){//新增部门
				this.modelShow = true
			},
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            handleSubmit(name) {//验证表单
               this.$refs[name].validate((valid) => {
                    if (valid) {//验证通过
                    	if(name == 'formValidate'){
                        	this.addDepartment()
                    	}else{
                    		this.editDepartment()
                    	}
                    } else {//验证不通过
                        this.$Message.error('请按要求完善表单!');
                        this.handleReset('formValidate')
                        this.handleReset('formValidate1')
                    }
                })
            },
			checkSure(){//点击弹窗确定 验证表单
				this.handleSubmit('formValidate')
			},
			checkSure1(){//点击弹窗确定 验证表单
				this.handleSubmit('formValidate1')
			},
			getUserList(){//获取管理员列表
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/getUser',
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			this.managerList = []
	    			if(res.data.code == 200){
	    				this.managerList = res.data.userList
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			edit(row){//点击修改按钮
				this.formValidate1.id = row.departmentId
				this.formValidate1.name = row.departmentName
				this.formValidate1.manager = row.departmentName
				this.modelEdit = true
			},
			editDepartment(){//提交修改表单
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/modifyDepartment',
	    			data:{
	    				departmentId:that.formValidate1.id,
	    				departmentName:that.formValidate1.name,
	    				departmentPrincipalId:that.formValidate1.manager
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.$Message.success(res.data.msg);
	    				this.getDepartment()
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			remove(row){//删除部门
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/deleteDepartment',
	    			data:{
	    				departmentId:row.departmentId,
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.$Message.success(res.data.msg);
	    				this.getDepartment()
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			addDepartment(){//提交新增部门表单
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/addDepartment',
	    			data:{
	    				departmentName:that.formValidate.name,
	    				departmentPrincipalId:that.formValidate.manager
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.$Message.success(res.data.msg);
	    				this.getDepartment()
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			goSearch(){//搜索部门
				if(this.search == ''){this.getDepartment();return}
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/selectDepartmentName',
	    			data:{
	    				departmentName:that.search
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.datas = []
	    				res.data.departmentList.forEach(item=>{
	    					if(!item.departmentPrincipalName){
	    						item.departmentPrincipalName = '暂无'
	    					}
	    					this.datas.push(item)
	    				})
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			getDepartment(){ // 获取部门列表
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/getDepartment',
	    			data:{
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.datas = []
	    				res.data.departmentList.forEach(item=>{
	    					if(!item.departmentPrincipalName){
	    						item.departmentPrincipalName = '暂无'
	    					}
	    					this.datas.push(item)
	    				})
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			}
		},
		created(){
			this.userType=this.LS.getLS('userType')
			this.getUserList()
			this.getDepartment()
		}
	}
</script>

<style scoped>
.right-box{
	width: 100%;
	height: 100%;
	position: relative;
}
.addBtn{
	position: absolute;
	top: -10px;
	right: 20px;
}
.page-nav{
	width: 100%;
	display: -webkit-flex;
	display: -ms-flexbox;
	display: -moz-box;
	display: flex;
	justify-content: center;
	align-content: center;
	margin-top: 10px;
}
.search-input{
	width:40%;
	position: absolute;
	top: -10px;
	left: 50%;
	transform: translateX(-50%);
	-webkit-transform: translateX(-50%);
}
</style>